﻿using System.Collections.Generic;

namespace CXW.Core.Sharp.Services
{
    /// <summary></summary>
    public class CommandWrapper
    {
        static Dictionary<string, ICommand> dic = new Dictionary<string, ICommand>();
        /// <summary>ICommand 对象实例化</summary>
        /// <param name="codon"></param>
        /// <returns></returns>
        public static ICommand CreateCommand(Codon codon, object owner)
        {
            ICommand menuCmd = dic.ContainsKey(codon.Id) ? dic[codon.Id] : null;
            if (menuCmd == null)
            {
                menuCmd = (ICommand)codon.AddIn.CreateObject(codon.Properties["class"]);
                if (menuCmd != null)
                {
                    menuCmd.Owner = owner;
                    dic.Add(codon.Id, menuCmd);

                    AbstractMenuCommand absMenu = menuCmd as AbstractMenuCommand;
                    if (absMenu != null)
                    {
                        absMenu.InfoTip = codon.Properties["infotip"];
                        absMenu.Language = codon.Properties["language"];
                        absMenu.Label = LanguageHelper.GetText(codon);
                        absMenu.CodonId = codon.Id;
                        absMenu.CfgName = codon.Properties["cfgname"].Length == 0 ? codon.Id : codon.Properties["cfgname"];
                        absMenu.Args = codon.Properties["args"];
                    }
                }
            }
            return menuCmd;
        }
    }
}
